1. /* sstring.cpp by K.Tsuru */
  2. // function ID = 601, 602, 603 DRADIX
  3. /***************************************************************************
  4. StringToNumber class
  5. Provides a conversion string to number.
  6. It identifies a number which has a point or an exponent with the REAL type,
  7. other with the DEC_INT one, however it can be substituted for both the SLong
  8. and SDouble variable.
  9. *****************************************************************************/
  10. #ifndef SN_H
  11. #include "sn.h"
  12. #endif
  13. /*
  14. It removes the sign, exponent, etc.
  15. */
  16. // function ID = 601
  17. void StringToNumber::GetDigit(const char* src){
  18. uint buffSize = strlen(src)+1;
  19. char* buff = new char[buffSize]; //work area
  20. char* pt;
  21. int c, sgn;
  22. uint k = 0;
  23. intLen = decLen = expLen = 0;
  24. //It removes the top spaces, tabs, zeros and sign.
  25. do{
  26. c = *(src++);
  27. } while( c && (!isdigit(c)) && c != '-' && c != '+' && c != '.');
  28. //It detects the sign.
  29. switch(c){
  30. // *src points the next of sign.
  31. case '+' : sgn = 1; break;
  32. case '-' : sgn = -1; break;
  33. case '\0' : snSign = 0; goto Ret;
  34. default : sgn = 1; src--; break; //a digit, back
  35. }
  36. snSign = sgn;
  37. //It detects an integral part.
  38. SetType(DEC_INT);
  39. pt = buff;
  40. do{
  41. c = *(src++);
  42. if(isdigit(c)){
  43. *(pt++) = c; k++;
  44. }
  45. } while( c && !( c == 'e' || c == 'E' || c == '.') );
  46. //If k == 0 the intPart[] is empty.
  47. *pt = '\0'; intPart = new char[k+1];
  48. strcpy(intPart, buff); intLen = k;
  49. if(c == '\0') goto Ret; //integral part only
  50. //It detects a decimal part.
  51. SetType(REAL);
  52. pt = buff; //put into buff[]
  53. k = 0;
  54. while( c && !( c == 'e' || c == 'E' ) ){
  55. c = *(src++);
  56. if(isdigit(c)){
  57. *(pt++) = c; k++;
  58. } else if(c == '.') SetError( SYNTAX_ERR,"too many point.", 61);
  59. }
  60. if(k){
  61. *pt = '\0'; decPart = new char[k+1];
  62. strcpy(decPart, buff); decLen = k;
  63. }
  64. if(c == '\0') goto Ret; // "iii.ddddd" form
  65. //It detects an exponent.
  66. //It does not check that the value can be expressed by a long variable or not.
  67. expLen = strlen(src);
  68. expPart = new char[expLen + 1];
  69. strcpy(expPart, src);
  70. Ret:
  71. delete[] buff;
  72. }
  73. static int rest(long s, int q){
  74. if( (s % q) == 0 ) return 0;
  75. return int(q - s % q);
  76. }
  77. // function ID = 602
  78. static const char* func = "StringToNumber";
  79. void StringToNumber::Analyze(){
  80. fType V = fTypeMax;
  81. char* pt;
  82. uint num_len = intLen + decLen; //length of number part
  83. uint total_len = num_len + expLen, k;
  84. if( !total_len ) V = 0; // strNum[] is empty
  85. else if( (intLen <= (uint)DFIGURES) && (Type() == DEC_INT) ){
  86. //The length of strNum is is smaller than four.
  87. //The sign in the intPart was already removed.
  88. V = atoF(intPart);
  89. if(V == 0) snSign = ZERO;
  90. }
  91. if( V < Radix() ){ //one figure integer
  92. valloc(minArraySize, 0); //sign = 0;
  93. figure[0] = V; SetSign(snSign);
  94. return;
  95. }
  96. //It detects the length of integral and decimal part.
  97. //V = aaa.bbbbcccc...zzzz * 10^x
  98. char* buff = new char[num_len+2*DFIGURES+1]; //work area
  99. *buff = '\0'; //Let be empty to enable to use "strcat" even if add0==0 in below.
  100. if(Type() == REAL){
  101. long exp;
  102. int add0;
  103. //It converts into V = [0.]00aa|abbb|bccc|cddd|....|z000 * DRADIX^snRDXExp.
  104. exp = expLen ? atol(expPart) : 0;
  105. exp += (long)intLen; // [0.]aaabbb...*10^(e+i)
  106. //the number of top zeros, DFIGURES > add0 >= 0
  107. add0 = exp > 0 ? rest(exp, DFIGURES) : (int)(labs(exp) % DFIGURES);
  108. if(add0){ // add0 > 0
  109. exp += add0;
  110. pt = buff;
  111. while(add0--) *(pt++) = '0';
  112. *pt = '\0';
  113. }
  114. //It copies the integral and decimal part continuously.
  115. strcat(buff, intPart); // intPart != NULL
  116. if(decPart != NULL) strcat(buff, decPart);
  117. //It adds zeros to the end.
  118. k = strlen(buff);
  119. if( ( add0 = rest(k, DFIGURES) ) != 0 ){ // k > 0 --> add0 >=0
  120. pt = strchr(buff, 0);
  121. while(add0--) *(pt++) = '0';
  122. *pt = '\0';
  123. }
  124. //the exponent in DRADIX.
  125. snRdxExp = int(exp/DFIGURES);
  126. num_len = strlen(buff);
  127. } else { //DEC_INT type
  128. int addspc; //the number of top spaces
  129. num_len = strlen(intPart);
  130. if( (addspc = rest(num_len, DFIGURES)) != 0 ){
  131. pt = buff;
  132. while(addspc--) *(pt++) = ' ';
  133. *pt = '\0';
  134. } // *buff = '\0';
  135. strcat(buff, intPart); // intPart != NULL
  136. num_len = strlen(buff);
  137. }
  138. //It puts into figure[] from top.
  139. char fig[DFIGURES + 1]; //work area
  140. uint rdx_figs;
  141. // rdx_figs < INT_MAX because num_len < UINT_MAX has been checked.
  142. pt = buff;
  143. rdx_figs = max(num_len/DFIGURES, 1u);
  144. #ifndef NDEBUG
  145. assert(num_len && !(num_len % DFIGURES) );
  146. #endif
  147. //Figures in DRADIX num_len is a multiple of DFIGURES.
  148. if(Type() == DEC_INT){
  149. if(rdx_figs > MaxSize()) SetError(OVERFLOW_ERR, func, 602);
  150. int j;
  151. valloc(rdx_figs, 0); //It allocates the memory of figure[].
  152. for( j = rdx_figs - 1; j >= 0 ; j--){
  153. strncpy(fig, pt, DFIGURES);
  154. fig[DFIGURES] = '\0';
  155. figure[j] = atoF(fig);
  156. pt += DFIGURES;
  157. }
  158. }else{ //REAL
  159. uint i, vsz = min(rdx_figs+1u, MaxSize()); //It adds one for figure[0].
  160. valloc(vsz, 0);
  161. for( i = 1u; i < vsz; i++){
  162. strncpy(fig, pt, DFIGURES);
  163. fig[DFIGURES] = '\0';
  164. figure[i] = atoF(fig);
  165. pt += DFIGURES;
  166. }
  167. }
  168. delete[] buff;
  169. SetSign(snSign);
  170. CheckArray(602);
  171. }
  172. // function ID = 603
  173. // destructor
  174. StringToNumber::~StringToNumber(){
  175. delete[] intPart; delete[] decPart; delete[] expPart;
  176. }

sstring.cpp : last modifiled at 2017/03/17 11:10:49(5,441 bytes)
created at 2016/04/11 11:36:47
The creation time of this html file is 2017/10/27 10:59:17 (Fri Oct 27 10:59:17 2017).